let ImageView = {};
//下面用于图片上传预览功能
(function($){
    $.setImagePreview = function(id, preview, localImage) {
        let docObj = document.getElementById(id);
        if ($.checkImgType(docObj)){
            let imgObjPreview=document.getElementById(preview);
            if(docObj.files && docObj.files[0]){
                //火狐下，直接设img属性
                imgObjPreview.style.display = "block";
                imgObjPreview.style.width = "300px";
                imgObjPreview.style.height = "180px";

                //火狐7以上版本
                imgObjPreview.src = window.URL.createObjectURL(docObj.files[0]);
            }else{
                //IE下，使用滤镜
                docObj.select();
                let imgSrc = document.selection.createRange().text;
                let localImagId = document.getElementById(localImage);
                //必须设置初始大小
                localImagId.style.width = "300px";
                localImagId.style.height = "180px";
                //图片异常的捕捉，防止用户修改后缀来伪造图片
                try{
                    localImagId.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
                    localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc;
                }catch(e){
                    return false;
                }
                imgObjPreview.style.display = "none";
                document.selection.empty();
            }
        }
        return true;
    }

    $.checkImgType = function(ths){
        if (ths.value == "") {
            alert("请上传图片");
            return false;
        } else {
            if (!/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(ths.value)) {
                alert("图片类型必须是.gif,jpeg,jpg,png中的一种");
                ths.value = "";
                return false;
            }
        }
        return true;
    }

    $.checkForm = function(){
        let yhdId = document.getElementById("yhdId").value;
        if(yhdId == ""){
            alert("请选择隐患点！");
            return false;
        }
        document.getElementById("formSave").action = "save.html";
    }
})(ImageView);